import type { Dispatch } from "@reduxjs/toolkit";
import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

// 定义store
const channelStore = createSlice({
  name: "channel",
  initialState: {
    // 初始化channel列表
    list: [],
  },
  reducers: {
    // 设置channel列表
    setList: (state, action) => {
      state.list = action.payload;
    },
  },
});

// 异步请求
const { setList } = channelStore.actions;
const getListAction = () => {
  return async (dispatch: Dispatch) => {
    const res = await axios.get("https://geek.itheima.net/v1_0/channels");
    dispatch(setList(res.data.data.channels));
  };
};

//导出
export default channelStore.reducer;
export { getListAction };
